package com.yupi.yupicturebackend.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.yupicturebackend.common.BaseResponse;
import com.yupi.yupicturebackend.common.ResultUtils;
import com.yupi.yupicturebackend.exception.BusinessException;
import com.yupi.yupicturebackend.exception.ErrorCode;
import com.yupi.yupicturebackend.model.dto.picture.PictureCommentQueryRequest;
import com.yupi.yupicturebackend.model.dto.picture.PictureCommentRequest;
import com.yupi.yupicturebackend.model.entity.PictureComment;
import com.yupi.yupicturebackend.model.entity.User;
import com.yupi.yupicturebackend.model.vo.PictureCommentRootVo;
import com.yupi.yupicturebackend.model.vo.PictureCommentVo;
import com.yupi.yupicturebackend.service.PictureCommentService;
import com.yupi.yupicturebackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/pictureComment")
public class PictureCommentController {
    @Resource
    private UserService userService;
    @Autowired
    private PictureCommentService pictureCommentService;

    @PostMapping("/add")
    public BaseResponse<PictureCommentVo> addPictureComment(@RequestBody PictureCommentRequest pictureCommentRequest, HttpServletRequest request) {
        if(request== null||pictureCommentRequest== null){
            return new BaseResponse<>(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        // 校验身份
        if(pictureCommentRequest.getId()!= null)
        {
            if(loginUser==null){
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
            if(!loginUser.getId().equals(pictureCommentRequest.getUserId())){
                throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户与评论ID不一致");
            }
        }
        PictureComment pictureComment = pictureCommentService.saveOrUpdate(pictureCommentRequest);
        PictureCommentVo pictureCommentVo = PictureCommentVo.objToVo(pictureComment);
        return ResultUtils.success(pictureCommentVo);
    }
    
    /**
     * 分页获取图片评论列表
     *
     * @param pictureCommentQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/list/page/")
    public BaseResponse<Page<PictureCommentRootVo>> listPictureCommentByPage(PictureCommentQueryRequest pictureCommentQueryRequest, HttpServletRequest request) {
        if (pictureCommentQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 获取当前登录用户
        User loginUser = userService.getLoginUser(request);
        // 如果未登录，也可以查看公开评论
        Page<PictureCommentRootVo> pictureCommentPage = pictureCommentService.getPictureCommentRootVo(pictureCommentQueryRequest);
        return ResultUtils.success(pictureCommentPage);
    }
}